package com.qire.manhua.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewParent;

import com.qire.common.utils.EquipmentInfoUtil;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

/**
 * 横向布局的 RecyclerView 独立出来是为了之后可以更好的在自定义控件上扩展也可以避免一些常规标准设置。
 */
public class HorizontalRecyclerView extends RecyclerView {

    /** 横向Item间距不包括收尾两边距离，收尾两边距离应该交给外边距来控制 */
    private int space = EquipmentInfoUtil.dip2px(4.5f);

    private ViewParent wrapperViewPager;

    private int mTouchSlop;

    private int move_x, move_y;

    public HorizontalRecyclerView(Context context) {
        this(context, null);
    }

    public HorizontalRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        // 设置横向布局
        setLayoutManager(new LinearLayoutManager(context, HORIZONTAL, false));
        // 添加子项间距处理
        post(() -> addItemDecoration(new HorizontalItemDecoration()));
        // 获取系统默认的 View 相关配置
        final ViewConfiguration vc = ViewConfiguration.get(context);
        // 设置触摸脏区
        mTouchSlop = vc.getScaledTouchSlop();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        if(wrapperViewPager == null) {
            wrapperViewPager = this;
        }

        while (!(wrapperViewPager instanceof ViewPager)) {
            wrapperViewPager = wrapperViewPager.getParent();
            if(wrapperViewPager == null){
                break;
            }
        }

        if(wrapperViewPager != null) {
            wrapperViewPager.requestDisallowInterceptTouchEvent(true);
        }

        return super.dispatchTouchEvent(ev);

    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN :
                move_x = (int) e.getX();
                move_y = (int) e.getY();
                this.getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE :
                int y = (int) e.getY();
                int x = (int) e.getX();
                ViewParent parent = this.getParent();
                if(Math.abs(y - move_y) > mTouchSlop && Math.abs(x - move_x) < mTouchSlop * 2) {
                    parent.requestDisallowInterceptTouchEvent(false);
                }else {
                    parent.requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_UP :
                break;
        }
        return super.onTouchEvent(e);
    }

    /**
     * 设置 item 之间间距，仅作用在两 Item 之间，单边无效，间距为设置值得2倍，即左右
     * @param space
     */
    public void setItemSpace(int space) {
        this.space = space;
    }

    private class HorizontalItemDecoration extends ItemDecoration {

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int itemCount = state.getItemCount();
            int currentIndex = parent.getChildAdapterPosition(view);
            if (currentIndex != 0) {
                // 排除第一个项目不设置左间距
                outRect.left = space;
            }
            // 从0开始，不包含最后一个
            if(currentIndex < (itemCount - 1)) {
                // 排除最后一个项目不设置右间距
                outRect.right = space;
            }
        }
    }

}

